export default {
    props: {
        saveHideColumnsKey: String, //显示列储存key值
    },
    data() {
        return {
            columnSelector: {
                value: [],
                hide: []
            }
        }
    },
    computed: {
        isColumnsShowAll() {
            return this.table.cols.every(col => col.hide !== true)
        }
    },
    methods: {
        getStorageHideColumns() {
            const hideColumns = this.$getHideColumns(this.saveHideColumnsKey)
            this.columnSelector.hide = hideColumns || []
            this.initShowColumns()
        },
        columnsOnChange(value, prop) {
            if (value === false) {
                this.columnSelector.hide.every(item => item !== prop) && this.columnSelector.hide.push(prop)
            } else {
                const index = this.columnSelector.hide.findIndex(item => item === prop)
                index >= 0 && this.columnSelector.hide.splice(index, 1)
            }
        },
        initShowColumns() {
            this.table.cols = this.table.cols.map(col => {
                return {
                    ...col,
                    hide: this.columnSelector.hide.some(item => item === col.prop)
                }
            })

            this.$saveHideColumns(this.saveHideColumnsKey, this.columnSelector.hide)
        },
        $saveHideColumns(key, value) {
            if (typeof this.saveHideColumnsKey !== 'string' || !this.saveHideColumnsKey) {
                return
            }

            this.$storage.set(key, {
                userId: this.$user.id || 'anonymous',
                value
            })
        },
        $getHideColumns(key) {
            const HideColumns = this.$storage.getData(key)
            if (!HideColumns || !HideColumns.value || (HideColumns.userId !== this.$user.id && HideColumns.userId !== 'anonymous')) {
                return []
            }

            return HideColumns.value
        }
    }
}